package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.RecommendAlumnusActivity;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.ViewUtil;
import com.xgkj.diyiketang.weidget.RoundImageView;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/3/24.
 */
public class RecommendAlumnusAdapter extends BaseAdapter {
    private Context mContext;
    private List<EatShowUserInfo> mList = new ArrayList<EatShowUserInfo>();

    public RecommendAlumnusAdapter(Context context) {
        mContext = context;
    }

    public void updateData(List<EatShowUserInfo> list) {
        mList.addAll(list);
        notifyDataSetChanged();
    }

    public void clearData() {
        mList.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        if (mList.size() > 0) {
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.alumnuslist_item, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        if (mList != null) {
            initializeViews(mList, (ViewHolder) convertView.getTag(), position);
        }

        return convertView;
    }

    private void initializeViews(final List<EatShowUserInfo> mList, ViewHolder holder, final int position) {
        final EatShowUserInfo info = mList.get(position);
        String headUrl = info.getUserLogo();
        if (headUrl != null && headUrl.length() > 0) {
            ViewUtil.Size size = ViewUtil.getHeadImageSize(mContext, 40);
            String cache_key = ImageCache.getInstance(mContext).getCacheKey(headUrl, size);
            Bitmap cache_bitmap = ImageCache.getInstance(mContext).getBitmapFromMemoryCache(cache_key);
            if (cache_bitmap != null) {
                holder.headImg.setImageBitmap(cache_bitmap);
            } else {
                holder.headImg.setImageResource(R.drawable.default_head_image);
                holder.headImg.setTag(headUrl);
                ImageCache.getInstance(mContext).loadBitmapToView(holder.headImg, headUrl, size);
            }
        } else {
            holder.headImg.setImageResource(R.drawable.default_head_image);
        }
//        holder.headImg.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                String uid = info.getUserId();
//                Intent intent = new Intent(mContext, HisDetailActivity.class);
//                intent.putExtra("USERID", uid);
//                mContext.startActivity(intent);
//            }
//        });
        try {
            holder.nickname.setText(URLDecoder.decode(info.getNickName(), "utf-8"));
            holder.personSignature.setText(URLDecoder.decode(info.getUserSign(), "utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        holder.addBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onAddClick(info.getUserId(), position);
                    mList.remove(position);
                    notifyDataSetChanged();
                }
            }
        });

    }

    public interface OnAdapterClickListener {
        public void onAddClick(String userId, int position);
    }

    private OnAdapterClickListener mListener;

    public void setOnAdapterClickListener(RecommendAlumnusActivity listener) {
        mListener = listener;
    }


    protected class ViewHolder {
        private RoundImageView headImg;
        private ImageButton addBtn;
        private TextView nickname;
        private TextView personSignature;
        private TextView alreadyReceive;

        public ViewHolder(View view) {
            headImg = (RoundImageView) view.findViewById(R.id.head_img);
            addBtn = (ImageButton) view.findViewById(R.id.add_alumnus);
            alreadyReceive = (TextView) view.findViewById(R.id.already_receive);
            nickname = (TextView) view.findViewById(R.id.nickname);
            personSignature = (TextView) view.findViewById(R.id.person_signature);
        }
    }
}
